home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8349 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: tech.cftnet.com!not-for-mail
  2. From: wcowley@cftnet.com (Wes Cowley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Does this create memory leak?...
  5. Date: 17 Feb 1996 00:18:53 GMT
  6. Organization: CFTnet
  7. Message-ID: <4g36te$fvs@tech.cftnet.com>
  8. References: <3123DF68.1677@sierra.net>
  9. NNTP-Posting-Host: ppp251_6.cftnet.com
  10. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  11.  
  12. T Colwell (snowbull@sierra.net) wrote:
  13.  
  14. :    CAT::CAT()
  15. :    {
  16. :         itsAge = new int;
  17. :         itsWeight = new int;
  18. :         *itsAge = 5;
  19. :         *itsWeight = 9;
  20. :    }
  21. :   CAT CAT::operator=(const CAT & rhs)
  22. :   {
  23. :      if (this == &rhs)
  24. :         return *this;
  25. :         itsAge = new int;
  26. :         itsWeight = new int;
  27. :         *itsAge = rhs.GetAge();
  28. :         *itsWeight = rhs.GetWeight();
  29. :   }
  30. : The assignment operator reinitializes itsAge and itsWeight to 
  31. : point to new heap addresses, 
  32. : what happens to the addresses they were pointing to before?  
  33. : Doesn't this create stranded addresses (leaks)?
  34.  
  35. Yep.  You need to delete the previous pointers in your assignment 
  36. operator before you overwrite them.
  37.  
  38. Wes
  39.